// components/blog-control/blog-control.js
let userInfo = {}
const db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    loginShow: false,
    modalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */

  methods: {
    onComment() {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                this.setData({
                  modalShow: true
                })
              }
            })
          } else {
            this.setData({
              loginShow: true
            })
          }
        }
      })
    },
    onLoginSuccess() {
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true
        })
      })
    },
    onLoginFail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: ''
      })
    },
    onInput(event) {
      this.setData({
        content: event.detail.value
      })
    },
    onSend(event) {
      console.log(event)
      let content = this.data.content
      /*       let formId = event.detail.formId */
      console.log(event)

      if (content.trim() == '') {
        wx.showModal({
          title: '评论不可为空',
          content: ''
        })
        return
      }
    wx.showLoading({
     title: '评价中',
      mask: true
      })
      const templateId = 'WZ3EC8uFfZzXJvbbalJIsAFvt027fG_1tMjaNgAz-Rs'
      wx.cloud.callFunction({
        name:'sendMessage',
        data:{
          content,
          blogId:this.data.blogId
        }
      })
 db.collection('blog-comment').add({
        data: {
          content,
          createTime: db.serverDate(),
          blogId: this.properties.blogId,
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl
        }
      })


      wx.requestSubscribeMessage({
        tmplIds: [templateId],
        complete: (res) => {
  
        }
      })
     
      wx.showToast({
        title: '评价成功',
      }).then((res) => {
        this.setData({
          modalShow: false
        }),
        this.triggerEvent('refresh')
      })
    }
  }
})